/*
 * @Author: Leo.wang wanglizhigs@163.com
 * @Date: 2024-06-10 22:55:48
 * @LastEditors: Leo.wang wanglizhigs@163.com
 * @LastEditTime: 2024-06-12 23:06:49
 * @FilePath: /chat-ai-plus/src/components/Popup/index.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { createVNode, getCurrentInstance, render, nextTick } from "vue";
import PopupConstructor from "./Popup.vue";

function usePopup() {
  let container;

  const close = () => {
    render(null, container);
    document.body.removeChild(container);
    container = undefined;
  }


  function getPopupInstance(instance) {
    if (container) {
      return;
    }
    const props = {
      onVanish: close
    }
    const appContext1 = getCurrentInstance();
    const appContext = instance.appContext;
    container = document.createElement("div");
    const vNode = createVNode(PopupConstructor, props);
    vNode.appContext = appContext;
    render(vNode, container);
    document.body.append(container);
    return vNode;
  }

  const open = (instance) => {
    getPopupInstance(instance);
  }

  return {
    open,
    close
  };
}
const popup = usePopup();
export default popup
